package com.mypaper.main;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

import com.mypaper.dag.DAG;
import com.mypaper.dag.DAGGenerator;
import com.mypaper.list.ListPriority;
import com.mypaper.schedule.HEFTSchedule;
import com.mypaper.schedule.HLDSchedule;
import com.mypaper.schedule.OptimizeSchedule;
import com.mypaper.schedule.RDDSchedule;

public class ScheduleTwo {

	/**
	 * @param args
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException{	
		//Group 1
		/*short procNum=4,vertexNum=13;
			float[][] vertexWeight={{3,2,1,2},//1
					{1,3,1,3},//2
					{2,1,3,2},//3
					{2,3,4,1},//4
					{1,1,1,1},//5
					{5,8,7,4},//6
					{2,1,2,3},//7
					{4,3,2,3},//8
					{1,3,2,2},//9
					{6,3,4,7},//10
					{2,1,3,2},//11
					{2,3,4,1},//12
					{2,1,2,3}};//13
			float[][] edgeWeight={{0,0,4,2,0,0,0,0,0,0,0,0,0},
					{0,0,1,0,0,0,0,6,0,0,0,0,0},
					{0,0,0,0,2,0,9,0,0,0,0,0,0},
					{0,0,0,0,1,5,2,1,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,3,0,0,0,0},
					{0,0,0,0,0,0,0,0,2,2,4,0,0},
					{0,0,0,0,0,0,0,0,0,3,0,8,0},
					{0,0,0,0,0,0,0,0,0,0,7,9,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,4},
					{0,0,0,0,0,0,0,0,0,0,0,0,1},
					{0,0,0,0,0,0,0,0,0,0,0,0,1},
					{0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0}};
		
		/*short procNum=4,vertexNum=10;
		float[][] vertexWeight={{14,46,19,82},
			{28,97,29,78},
			{86,8,95,82},
			{95,79,2,44},
			{7,93,17,55},
			{21,87,9,50},
			{10,81,32,96},
			{81,50,29,37},
			{2,29,67,67},
			{26,74,25,37}};
		float[][] edgeWeight={{0,78,88,23,0,0,0,0,0,0},
				{0,0,0,0,0,81,32,0,0,0},
				{0,0,0,0,71,89,0,0,0,0},
				{0,0,0,0,11,0,0,0,0,0},
				{0,0,0,0,0,0,0,67,0,0},
				{0,0,0,0,0,0,0,66,97,0},
				{0,0,0,0,0,0,0,22,51,0},
				{0,0,0,0,0,0,0,0,0,20},
				{0,0,0,0,0,0,0,0,0,57},
				{0,0,0,0,0,0,0,0,0,0}};*/
		
		
		/*short procNum=3, vertexNum=10;
		float[][] vertexWeight={
				{22,15,17},
				{15,25,5},
				{22,5,9},
				{19,19,16},
				{16,20,24},
				{16,14,9},
				{15,12,21},
				{9,21,3},
				{5,6,25},
				{10,17,24}};
		
		float[][] edgeWeight={
				{0,18,10,6,10,14,0,0,0,0},
				{0,0,0,0,0,0,0,18,0,0},
				{0,0,0,0,0,0,22,0,0,0},
				{0,0,0,0,0,0,14,26,22,0},
				{0,0,0,0,0,0,0,0,10,0},
				{0,0,0,0,0,0,0,14,0,0},
				{0,0,0,0,0,0,0,0,0,18},
				{0,0,0,0,0,0,0,0,0,10},
				{0,0,0,0,0,0,0,0,0,14},
				{0,0,0,0,0,0,0,0,0,0}};*/
	
				
		//Group 2
		/*short procNum=4,vertexNum=8;
		float[][] vertexWeight={{1,1,2,1},{3,2,4,2},{5,6,3,4},{2,4,4,2},
				{4,8,7,8},{3,3,1,2},{5,5,5,5},{1,2,2,2}};
		float[][] edgeWeight={{0,2,1,4,1,0,0,0},
				{0,0,0,0,0,8,0,0},
				{0,0,0,0,0,7,0,0},
				{0,0,0,0,0,2,10,0},
				{0,0,0,0,0,0,8,0},
				{0,0,0,0,0,0,0,5},
				{0,0,0,0,0,0,0,7},
				{0,0,0,0,0,0,0,0}};*/
		//Group 3
		/*short procNum=4,vertexNum=13;
		float[][] vertexWeight={{3,2,4,2},//1
				{2,3,1,5},//2
				{2,1,3,2},//3
				{2,3,4,1},//4
				{4,2,3,5},//5
				{5,8,7,4},//6
				{2,1,2,3},//7
				{4,3,2,3},//8
				{1,3,2,2},//9
				{6,3,4,7},//10
				{2,1,3,2},//11
				{2,3,4,1},//12
				{2,1,2,3}};//13
		float[][] edgeWeight={{0,0,7,5,0,0,0,0,0,0,0,0,0},
				{0,0,3,0,0,0,0,6,0,0,0,0,0},
				{0,0,0,0,9,0,9,0,0,0,0,0,0},
				{0,0,0,0,6,5,2,3,0,0,0,0,0},	
				{0,0,0,0,0,0,0,0,4,0,0,0,0},
				{0,0,0,0,0,0,0,0,3,2,4,0,0},
				{0,0,0,0,0,0,0,0,0,3,0,8,0},
				{0,0,0,0,0,0,0,0,0,0,7,9,0},
				{0,0,0,0,0,0,0,0,0,0,0,0,4},
				{0,0,0,0,0,0,0,0,0,0,0,0,3},
				{0,0,0,0,0,0,0,0,0,0,0,0,4},
				{0,0,0,0,0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0,0,0,0,0}};*/
			
			//Group 4
			/*short procNum=3,vertexNum=10;
			float[][] vertexWeight={{22,15,17},{15,25,5},{22,5,9},{19,19,16},{16,20,24},{16,14,9},
					{15,12,21},{9,21,3},{5,6,25},{10,17,24}};
			float[][] edgeWeight={{0,18,10,6,10,14,0,0,0,0},
					{0,0,0,0,0,0,0,18,0,0},
					{0,0,0,0,0,0,22,0,0,0},
					{0,0,0,0,0,0,14,26,22,0},
					{0,0,0,0,0,0,0,0,10,0,0},
					{0,0,0,0,0,0,0,14,0,0},
					{0,0,0,0,0,0,0,0,0,18},
					{0,0,0,0,0,0,0,0,0,10},
					{0,0,0,0,0,0,0,0,0,14},
					{0,0,0,0,0,0,0,0,0,0}};*/
					
		/*short procNum=4,vertexNum=10;
		float[][] vertexWeight={{93,49,43,67},
				{15,49,28,13},
				{4,19,9,77},
				{59,69,10,62},
				{4,59,51,28},
				{20,63,5,20},
				{4,86,30,69},
				{57,63,25,81},
				{64,51,86,24},
				{73,60,2,91}};
		float[][] edgeWeight={{0,1,24,0,0,0,0,0,0,0},
				{0,0,0,9,0,0,0,0,0,0},
				{0,0,0,27,0,0,0,0,0,0},
				{0,0,0,0,34,35,39,3,94,0},
				{0,0,0,0,0,0,14,0,6,0},
				{0,0,0,0,0,0,64,0,0,0},
				{0,0,0,0,0,0,0,0,0,20},
				{0,0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0,17},
				{0,0,0,0,0,0,0,0,0,0}};
		/*short procNum=3,vertexNum=10;
		float[][] vertexWeight={{7,8,9},	
				{11,14,17},	
				{12,15,18},
				{10,8,12},
				{5,7,6},	
				{9,7,5},	
				{6,8,7},	
				{14,12,10},	
				{10,8,6},	
				{11,13,15}};
		float[][] edgeWeight={{0,16,5,0,0,0,0,0,0,0},//1
				{0,0,0,14,7,0,0,0,0,0,0},//2
				{0,0,0,0,0,8,12,0,0,0},//3
				{0,0,0,0,0,0,0,14,0,0},//4
				{0,0,0,0,0,0,0,15,0,0},//5
				{0,0,0,0,0,0,0,0,16,0},//6
				{0,0,0,0,0,0,0,0,14,0},//7
				{0,0,0,0,0,0,0,0,0,8},//8
				{0,0,0,0,0,0,0,0,0,17},//9
				{0,0,0,0,0,0,0,0,0,0},//10
				};*/
		
		/*short procNum=4, vertexNum=10;
		float[][] vertexWeight={{41.55768f,92.530914f,31.081863f,26.105402f},	
				{3.6825438f,0.112928845f,32.29014f,47.66375f},	
				{41.15556f,52.51724f,13.917334f,80.55094f},
				{83.41398f,34.292557f,31.138144f,24.495823f},
				{5.4532723f,15.87779f,26.92726f,61.345318f},	
				{14.211864f,62.6854f,82.95699f,90.95349f},	
				{36.751556f,29.654287f,46.79768f,14.07127f},	
				{65.03401f,84.378296f,13.12177f,34.883797f},	
				{61.80082f,71.15469f,56.631786f,7.893405f},	
				{47.39652f,88.16024f,50.077183f,46.521797f}};
		
		float[][] edgeWeight={{0.0f,9.545559f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f},	
				{0.0f,0.0f,5.144695f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f},
				{0.0f,0.0f,0.0f,18.677172f,2.412945f,0.0f,0.0f,0.0f,0.0f,0.0f},	
				{0.0f,0.0f,0.0f,0.0f,0.0f,10.645799f,2.3019164f,0.0f,0.0f,0.0f},
				{0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,2.093592f,0.0f,0.0f,0.0f},
				{0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,17.419697f,0.0f,0.0f},
				{0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,3.7844737f,3.7692792f,0.0f},	
				{0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,3.1188953f},
				{0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,9.63106f},	
				{0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f}};*/
				
		
			/*short procNum=4, vertexNum=10;
			float[][] vertexWeight={{91,78,73,78},
					{65,69,42,13},
					{16,54,9,3},
					{67,43,58,7},
					{54,55,39,52},
					{57,15,7,62},
					{6,27,45,48},
					{67,88,46,16},
					{66,8,96,34},
					{24,42,47,46}};
		
			float[][] edgeWeight={{0,11,47,0,0,0,0,0,0,0},
					{0,0,0,65,0,0,0,0,0,0},
					{0,0,0,52,0,0,0,0,0,0},
					{0,0,0,0,72,4,0,88,0,0},
					{0,0,0,0,0,0,70,0,33,0},
					{0,0,0,0,0,0,0,0,0,64},
					{0,0,0,0,0,0,0,0,0,41},
					{0,0,0,0,0,0,0,0,0,6},
					{0,0,0,0,0,0,0,0,0,45},
					{0,0,0,0,0,0,0,0,0,0}};
				
		/*short procNum=4, vertexNum=10;
		float[][] vertexWeight={
				{60,0,59,84},
				{52,44,88,42}, 
				{52,7,6,88}, 
				{50,45,36,28},  
				{47,69,52,50},  
				{58,48,34,40},  
				{38,97,80,65},  
				{28,82,60,59},  
				{26,31,6,23},  
				{50,7,23,60}};  
	
		float[][] edgeWeight={
			{0,10,2,16,0,0,0,0,0,0},  
			{0,0,0,0,10,0,17,0,0,0},  
			{0,0,0,0,15,0,12,0,0,0},  
			{0,0,0,0,7,10,4,0,0,0},  
			{0,0,0,0,0,0,0,1,0,0},  
			{0,0,0,0,0,0,0,0,16,0}, 
			{0,0,0,0,0,0,0,13,0,0},  
			{0,0,0,0,0,0,0,0,0,15}, 
			{0,0,0,0,0,0,0,0,0,17}, 
			{0,0,0,0,0,0,0,0,0,0}};*/
		/*short procNum=4, vertexNum=10;
		float[][] vertexWeight=
		{{36,82,32,63},
	{73,35,17,92},
	{60,7,35,91},
	{72,36,29,72},
	{16,24,36,67},
	{34,95,61,5},
	{8,43,38,87},
	{8,38,96,50},
	{28,11,88,52},
	{95,50,19,69}};
	
		
		float[][] edgeWeight={{0,8,58,0,0,0,0,0,0,0},
	{0,0,0,0,19,54,0,0,0,0},
	{0,0,0,47,0,74,40,0,0,0},
	{0,0,0,0,0,0,0,59,99,0},
	{0,0,0,0,0,0,0,0,74,0},
	{0,0,0,0,0,0,0,1,77,0},
	{0,0,0,0,0,0,0,0,52,0},
	{0,0,0,0,0,0,0,0,0,82},
	{0,0,0,0,0,0,0,0,0,21},
	{0,0,0,0,0,0,0,0,0,0}};*/
				
				
		FileOutputStream fos=null;
		OutputStreamWriter osw;
		int count=0;
		DAGGenerator dg=null;
		while(count<50){
		boolean flag=true;
		//while(flag==true){
			System.out.println("count="+count);
			if(count==0){
				fos=new FileOutputStream("yu0_2.txt");
			}else if(count==50){
				fos=new FileOutputStream("yu0_5.txt");
			}else if(count==100){
				fos=new FileOutputStream("yu2.txt");
			}else if(count==150){
				fos=new FileOutputStream("yu5.txt");
			}
			if(count<50){
				dg=new DAGGenerator(16, 300,1, 0.2f);
			}
			else if(count<100){
				dg=new DAGGenerator(16, 300,1, 0.5F);		
			}
			else if(count<150){
				dg=new DAGGenerator(16, 300, 1, 2);
			}
			else if(count<200){
				dg=new DAGGenerator(16, 300,1, 5);			
			}
			
			
		//	fos=new FileOutputStream("yu0_2.txt");
			osw=new OutputStreamWriter(fos);
			
			//dg=new DAGGenerator(8, 20, 1, 1);
			
			short procNum=(short) dg.PROCNUM, vertexNum=(short)dg.TASKNUM;
			float[][] vertexWeight=dg.comp;
			float[][] edgeWeight=dg.comm;
			
			/*System.out.println("vertexWeight:");
			for(int i=0; i<vertexNum; i++){
				for(int j=0; j<procNum; j++){
					System.out.print(vertexWeight[i][j]+"  ");
				}
				System.out.println();
			}
			System.out.println();
			
			System.out.println("edgeWeight:");
			for(int i=0; i<vertexNum; i++){
				for(int j=0; j<vertexNum; j++){
					System.out.print(edgeWeight[i][j]+"  ");
				}
				System.out.println();
			}
			System.out.println();*/
				
			
			DAG dag=new DAG(vertexNum,procNum,vertexWeight,edgeWeight);	
			dag.calcBLPrior();
			ListPriority prior=dag.prior;
			dag.calcHCPrior();
			ListPriority priorHCP=dag.priorHCP;
			//HEFT算法
			float lowerResourse=0;
			for(int i=0; i<vertexNum; i++){
				float minWeightProc=Float.MAX_VALUE;
				for(int j=0; j<procNum; j++){
					if(minWeightProc>vertexWeight[i][j]){
						minWeightProc=vertexWeight[i][j];
					}
				}
				lowerResourse+=minWeightProc;
			}
			//prior.print();
			 //System.out.println("count="+count);
			HEFTSchedule heftSchedule=new HEFTSchedule(dag);//初始化
			heftSchedule.HEFT_Algorithm(dag);//调度
			float makespanHEFT=heftSchedule.makespan();//求makespan
			System.out.println("The makespan of HEFT is "+makespanHEFT);
			//HLD算法
			
			HLDSchedule hldSchedule=new HLDSchedule(vertexNum,procNum);//初始化调度	
			hldSchedule.HLD_Algorithm(vertexNum,procNum,prior,vertexWeight,edgeWeight);
			float makespanHLD=hldSchedule.makespan();
			System.out.println("The makespan of HLD is "+makespanHLD);
			float timeHLD=hldSchedule.timeCal(procNum);
			
			RDDSchedule rddSchedule=new RDDSchedule(vertexNum,procNum,dag);//初始化调度	
			rddSchedule.RDD_Algorithm(vertexNum,procNum,prior,vertexWeight,edgeWeight,dag);
			float makespanRDD=rddSchedule.makespan();
			System.out.println("The makespan of RDD is "+makespanRDD);
			float timeRDD=rddSchedule.timeCal(procNum);

			OptimizeSchedule optSchedule=new OptimizeSchedule(dag,dag.priorRever, dag.prior, rddSchedule, procNum, vertexNum);
			optSchedule.Optimize_Schedule(vertexNum, procNum, vertexWeight, edgeWeight, makespanRDD);
			//optSchedule.printSchedule(procNum, vertexNum);
			float makespanOpti=optSchedule.makespan(procNum);
			System.out.println("The makespan of optimal is "+makespanOpti);
			float timeOpt=optSchedule.timeCal(procNum);
			
			String s=String.valueOf(makespanHEFT+" "+lowerResourse+" "+makespanHLD+" "+timeHLD+" "+makespanRDD+" "+timeRDD+" "+makespanOpti+" "+timeOpt+"\n");       //把sum转化为字符
			try{
			osw.write(s);	
			osw.flush();
			}catch(Exception e){}//结果在文本文档中显示不出来*/
			count++;
			
			if(makespanHEFT<makespanHLD){
				flag=false;
			}
		}
	}
}
